package hu.vissy.notification;

import hu.vissy.notification.Notification.Level;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NotificationPool {

    private List<Notification> messages = new ArrayList<>();

    public void add(Notification message) {
        messages.add(message);
    }

    public int getSize() {
        return messages.size();
    }

    public boolean hasHigherLevel(Notification.Level threshold) {
        for (Notification n : messages) {
            if (n.getLevel().getWeight() >= threshold.getWeight()) {
                return true;
            }
        }
        return false;
    }

    public List<Notification> getMessages() {
        return Collections.unmodifiableList(messages);
    }

    public String printMessages() {
        return printMessagesAbove(Level._MIN);
    }


    public String printMessagesAbove(Level threshold) {
        StringBuilder sb = new StringBuilder();
        printMessageAbove(sb, threshold);
        return sb.toString();
    }

    public void printMessageAbove(StringBuilder sb, Level threshold) {
        for (Notification n : messages) {
            if (n.getLevel().getWeight() >= threshold.getWeight()) {
                sb.append(String.format("%7s %s\n", n.getLevel(), n.getMessage()));
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("NotificationPool [\n");
        for (Notification n : messages) {
            sb.append(String.format("%7s %s\n", n.getLevel(), n.getMessage()));
        }
        sb.append("]");
        return sb.toString();
    }

}
